<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户Id" prop="userId">
      <el-input v-model="dataForm.userId" placeholder="用户Id"></el-input>
    </el-form-item>
    <el-form-item label="用户名" prop="userName">
      <el-input v-model="dataForm.userName" placeholder="用户名"></el-input>
    </el-form-item>
    <el-form-item label="用户头像" prop="userAvatar">
      <el-input v-model="dataForm.userAvatar" placeholder="用户头像"></el-input>
    </el-form-item>
    <el-form-item label="博客Id" prop="blogId">
      <el-input v-model="dataForm.blogId" placeholder="博客Id"></el-input>
    </el-form-item>
    <el-form-item label="内容" prop="content">
      <el-input v-model="dataForm.content" placeholder="内容"></el-input>
    </el-form-item>
    <el-form-item label="评论时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="评论时间"></el-input>
    </el-form-item>
    <el-form-item label="父级Id" prop="parentId">
      <el-input v-model="dataForm.parentId" placeholder="父级Id"></el-input>
    </el-form-item>
    <el-form-item label="状态" prop="state">
      <el-input v-model="dataForm.state" placeholder="状态"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          userId: '',
          userName: '',
          userAvatar: '',
          blogId: '',
          content: '',
          createTime: '',
          parentId: '',
          state: ''
        },
        dataRule: {
          userId: [
            { required: true, message: '用户Id不能为空', trigger: 'blur' }
          ],
          userName: [
            { required: true, message: '用户名不能为空', trigger: 'blur' }
          ],
          userAvatar: [
            { required: true, message: '用户头像不能为空', trigger: 'blur' }
          ],
          blogId: [
            { required: true, message: '博客Id不能为空', trigger: 'blur' }
          ],
          content: [
            { required: true, message: '内容不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '评论时间不能为空', trigger: 'blur' }
          ],
          parentId: [
            { required: true, message: '父级Id不能为空', trigger: 'blur' }
          ],
          state: [
            { required: true, message: '状态不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/blog/comment/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.userId = data.blogComment.userId
                this.dataForm.userName = data.blogComment.userName
                this.dataForm.userAvatar = data.blogComment.userAvatar
                this.dataForm.blogId = data.blogComment.blogId
                this.dataForm.content = data.blogComment.content
                this.dataForm.createTime = data.blogComment.createTime
                this.dataForm.parentId = data.blogComment.parentId
                this.dataForm.state = data.blogComment.state
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/blog/comment/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'userId': this.dataForm.userId,
                'userName': this.dataForm.userName,
                'userAvatar': this.dataForm.userAvatar,
                'blogId': this.dataForm.blogId,
                'content': this.dataForm.content,
                'createTime': this.dataForm.createTime,
                'parentId': this.dataForm.parentId,
                'state': this.dataForm.state
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
